package com.zhugang.week04;

/**
 * @program algorithms
 * @description: reverseList
 * @author: chanzhugang
 * @create: 2022/06/24 00:03
 */
public class ReverseList2 {

    /**
     * 反转链表
     *
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        // 递归实现
        if (head == null || head.next == null) {
            // 终止条件
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;

    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}